// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod commands;
use log::{error, info};

fn main() {
    // 初始化日志 - 增强版配置
    let log_level = if cfg!(debug_assertions) {
        log::LevelFilter::Debug
    } else {
        log::LevelFilter::Info
    };

    env_logger::Builder::from_default_env()
        .filter_level(log_level)
        .init();

    info!("应用程序启动");

    tauri::Builder::default()
        // 注册插件
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_opener::init())
        // 注册所有命令处理器
        .invoke_handler(tauri::generate_handler![
            // 浏览器
            commands::browser::open_url,
            commands::browser::open_url_with_browser,
            commands::browser::detect_available_browsers,
            commands::parse_excel::parse_excel,
            commands::upload_file::upload_file,
            commands::chat_stream::chat_stream,
            commands::user_settings::get_user_settings,
            commands::user_settings::save_user_settings,
            commands::user_settings::test_database_connection,
            commands::user_settings::get_auto_login_users,
            commands::user_settings::get_users_from_database,
            commands::user_settings::get_users_from_file,
            commands::user_settings::generate_login_token,
            commands::user_settings::build_auto_login_url,
            commands::user_settings::record_login_click,
            commands::user_settings::get_login_ranking,
            commands::user_settings::get_user_stats,
            commands::user_settings::init_login_stats_db,
            // 自动更新
            commands::updater::check_for_updates,
            commands::updater::download_update,
            commands::updater::apply_update,
            // 日志管理
            commands::logger::log_system_event,
            commands::logger::get_logs,
            commands::logger::clear_logs,
            commands::logger::export_logs,
            // 数据库操作模块
            commands::database::execute_sql_query,
            commands::database::get_workflow_list,
            commands::database::get_workflow_xml,
            commands::database::test_db_connection,
            // AI
            commands::ai_api::generate_function_points,
            commands::ai_api::generate_cosmic_content,
            commands::ai_api::open_folder,
            // 概要设计生成
            commands::overview_design::generate_overview_design,
            // 网络请求
            commands::http_client::http_request,
            commands::http_client::http_get,
            commands::http_client::http_post,
        ])
        .run(tauri::generate_context!())
        .unwrap_or_else(|err| {
            error!("应用程序运行失败: {}", err);
            std::process::exit(1);
        });
}
